<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="文章标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'title', validatorRules.title]" placeholder="请输入文章标题"></p-input>
        </p-form-item>
        <p-form-item label="文章内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-editor v-decorator="['content',{trigger:'input'}]"/>
        </p-form-item>
        <p-form-item label="文章类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-category-select v-decorator="['articleType', validatorRules.articleType]" pcode="B03" placeholder="请选择文章类型" />
        </p-form-item>

      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JEditor from '@/components/jeecg/JEditor'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'

  export default {
    name: "FxArticleModal",
    components: { 
      JEditor,
      JCategorySelect
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          title: {rules: [
          ]},
          content: {rules: [
          ]},
          articleType: {rules: [
          ]},
        },
        url: {
          add: "/codedemoapi/codedemo/fxArticle/add",
          edit: "/codedemoapi/codedemo/fxArticle/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','title','content','articleType'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','title','content','articleType'))
      },
      handleCategoryChange(value,backObj){
        this.form.setFieldsValue(backObj)
      }

      
    }
  }
</script>